package com.carryGo.utils;


import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.rmi.ServerException;

/**
 * 验证码发送
 */
public class SendPhoneCodeUtil {

    private String phone;
    private String code = "";

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    /*public static void main(String[] args) {
        new SendPhoneCodeUtil("15073497708").send();
    }*/

    public SendPhoneCodeUtil(String phone) {
        this.phone = phone;
    }
    // {"Message":"1573497708invalid mobile number","RequestId":"E9FDB03F-00E6-4BBB-9B3F-ED6D27C4C0F8","Code":"isv.MOBILE_NUMBER_ILLEGAL"}
    // {"Message":"OK","RequestId":"5B48566C-255E-4E5F-84FB-6696E7DB1E3C","BizId":"116818994110362003^0","Code":"OK"}

    public void send() {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4G8AEJpuUu69U3ZUakvv", "WAwppA0bvPybuyUHsAUyxFWcJPMzVo");
        //DefaultProfile profile = DefaultProfile.getProfile("default", "", "");
        IAcsClient client = new DefaultAcsClient(profile);

        String code = vcode(); // 验证码
        //System.out.println(code);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", phone); // phone 电话号码
        request.putQueryParameter("SignName", "吃了没验证码"); // 签名名称
        request.putQueryParameter("TemplateCode", "SMS_195575807"); // 短信模板的CODE
        request.putQueryParameter("TemplateParam", "{\"code\":\""+ code +"\"}"); // code，需要发送的内容（验证码）
        try {
            CommonResponse response = client.getCommonResponse(request);
            //response.getData().
            //System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取6位随机的验证码
     * @return
     */
    public  String vcode(){
        code = "";
        for (int i = 0; i < 6; i++) {
            code = code + (int)(Math.random() * 9);
        }
        return code;
    }

}
